package cn.li.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.samples.mvc.basic.account.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value="/mvc")
public class DemoController {
	
	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
	
	/**
	 * get 请求 http   /account   默认方法
	*/
	@RequestMapping(method=RequestMethod.GET)
	public String getCreateForm(Model model) {
		logger.info("getCreateForm ... ");
		model.addAttribute(new Account());
		return "account/createForm";
	}
 
	@RequestMapping(method=RequestMethod.POST,value="/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Person person) {
		logger.info("index............");
		ModelAndView result = new ModelAndView();
		result.addObject("person",person);
		result.setViewName("/account/list");
		return result;
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/{pageNumber}/{pageSize}")
	public ModelAndView list(@PathVariable int pageNumber,@PathVariable int pageSize,
			@RequestBody Person person) {
		logger.info("----------------------"+pageNumber);
		logger.debug("RequestBody List,{}",person);
		HashMap<String,Object> model = new HashMap<String,Object>();
		model.put("person", person);
		ModelAndView mav = new ModelAndView("/personList",model );
		logger.info("mav==== "+mav);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/person")
	public ModelAndView create(@RequestBody Person person) {
		System.out.println("create..controller...............");
		System.out.println("create {}"+person);
		HashMap<String,Object> model = new HashMap<String,Object>();
		model.put("success", "true");
		return new ModelAndView("msg",model );
	}
	
	@RequestMapping(method=RequestMethod.PUT,value="/person")
	public ModelAndView update(@RequestBody Person person) {
		System.out.println("update {}"+person);
		HashMap<String,String> model = new HashMap<String,String>();
		model.put("success", "true");
		return new ModelAndView("msg",model );
	}
	
	@RequestMapping(method=RequestMethod.DELETE,value="/person")
	public ModelAndView delete(@RequestBody Person person) {
		logger.debug("delete {}",person);
		HashMap<String,String> model = new HashMap<String,String>();
		model.put("success", "true");
		return new ModelAndView("msg",model );
	}
}
